from sqlalchemy import Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from init.mysql import engine

Base = declarative_base()


class StockHistoryTrade(Base):
    __tablename__ = 'stock_history_trade'
    id = Column(Integer, primary_key=True, autoincrement=True)
    code = Column(String(6))
    t = Column(String(10))
    c = Column(String(20))
    zdf = Column(String(10))
    jlrl = Column(String(10))
    hsl = Column(String(10))
    qbjlr = Column(String(20))
    cddlr = Column(String(20))
    cddjlr = Column(String(20))
    ddlr = Column(String(20))
    ddjlr = Column(String(20))
    xdlr = Column(String(20))
    xdjlr = Column(String(20))
    sdlr = Column(String(20))
    sdjlr = Column(String(20))

    def __init__(self, code, t, c, zdf, jlrl, hsl, qbjlr, cddlr, cddjlr, ddlr, ddjlr, xdlr, xdjlr, sdlr, sdjlr):
        self.code = code
        self.t = t
        self.c = c
        self.zdf = zdf
        self.jlrl = jlrl
        self.hsl = hsl
        self.qbjlr = qbjlr
        self.cddlr = cddlr
        self.cddjlr = cddjlr
        self.ddlr = ddlr
        self.ddjlr = ddjlr
        self.xdlr = xdlr
        self.xdjlr = xdjlr
        self.sdlr = sdlr
        self.sdjlr = sdjlr

    @staticmethod
    def insert_or_update(code, data_list):
        session = sessionmaker(bind=engine)()
        for data in data_list:
            existing_stock_history_trade = session.query(StockHistoryTrade).filter_by(code=code, t=data['t']).first()
            if existing_stock_history_trade:
                existing_stock_history_trade.c = data['c']
                existing_stock_history_trade.zdf = data['zdf']
                existing_stock_history_trade.jlrl = data['jlrl']
                existing_stock_history_trade.hsl = data['hsl']
                existing_stock_history_trade.qbjlr = data['qbjlr']
                existing_stock_history_trade.cddlr = data['cddlr']
                existing_stock_history_trade.cddjlr = data['cddjlr']
                existing_stock_history_trade.ddlr = data['ddlr']
                existing_stock_history_trade.ddjlr = data['ddjlr']
                existing_stock_history_trade.xdlr = data['xdlr']
                existing_stock_history_trade.xdjlr = data['xdjlr']
                existing_stock_history_trade.sdlr = data['sdlr']
                existing_stock_history_trade.sdjlr = data['sdjlr']
            else:
                session.add(StockHistoryTrade(code=code, t=data['t'], c=data['c'], zdf=data['zdf'], jlrl=data['jlrl'],
                                              hsl=data['hsl'], qbjlr=data['qbjlr'], cddlr=data['cddlr'],
                                              cddjlr=data['cddjlr'], ddlr=data['ddlr'], ddjlr=data['ddjlr'],
                                              xdlr=data['xdlr'], xdjlr=data['xdjlr'], sdlr=data['sdlr'],
                                              sdjlr=data['sdjlr']))
        session.commit()
        session.close()


Base.metadata.create_all(engine)
